<template>
<div class="login">
  <!-- 2.0给el-form 添加ref属性,表示校验规则 -->
  <el-form :model="myform" class="container" :rules='formrules' ref='refForm'>
    <el-form-item class="avatar">
      <div>
          <img src="../assets/avatar.jpg" alt="">
      </div>
    </el-form-item>
  <el-form-item prop="username">
    <el-input v-model="myform.username" prefix-icon='myicon myicon-user'></el-input>
  </el-form-item>
  <el-form-item prop="password">
    <el-input v-model="myform.password" type="password"  prefix-icon='myicon myicon-key' @keydown.enter.native="submitForm('refForm')"></el-input>
  </el-form-item>
    <el-form-item>
        <el-button type="primary" class="login-btn" @click="submitForm('refForm')">登录</el-button>
   </el-form-item>
  </el-form>
  </div>
</template>
<script>
// 1.0引入postLogin必须使用{}来接收
import {postLogin} from '@/api'
export default {
  data () {
    return {
      myform: {
        username: 'admin',
        password: '123456'
      },
      formrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      // 2.1发请求前，先利用validate()方法对表单进行校验，validate在element-ui中已经封装好了
      this.$refs[formName].validate((valid) => {
        // 2.2参数valid是一个布尔类型值，为true表示校验通过，则可以进行下一步请求为false表示校验失败
        if (valid) {
          console.log('登陆了')
          // axios.post('http://localhost:8888/api/private/v1/login', {
          //   username: this.myform.username,
          //   password: this.myform.password
          // })
          //   .then(res => {
          //     if (res.data.meta.status === 200) {
          //       console.log('登陆成功')
          //       // 登陆成功 跳转到首页面
          //       this.$router.push({name: 'Home'})
          //     } else {
          //       this.$message.error('用户名或者密码')
          //     }
          //   })
          postLogin({ username: this.myform.username,
            password: this.myform.password})
            .then(res => {
              // index.js里面传递了一个data所以这边接收要少一个data
              if (res.meta.status === 200) {
                // 登陆成功token保存本地
                localStorage.setItem('mytoken', res.data.token)
                // 首页需要一个 ‘你好：某某某’某某某为名字  索引需要拿到name
                // console.log(res.data.name)
                // console.log(res.data.username)
                localStorage.setItem('username', res.data.username)
                // console.log(res.data.token)
                console.log('登陆成功')
                // 登陆成功 跳转到首页面
                this.$router.push({name: 'Home'})
              } else {
                this.$message.error(res.meta.status)
              }
            })
        } else {
          console.log('错误')
          this.$message.warning('请正确填写表单')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .login {
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: #2f4050;

    .container {
      position: absolute;
      left: 0;
      right: 0;
      width: 400px;
      padding: 0px 40px 15px 40px;
      margin: 200px auto;
      background: white;
      .avatar {
        position: relative;
        left: 50%;
        width: 120px;
        height: 120px;
        margin-left: -60px;
        margin-top: -60px;
        box-sizing: border-box;
        border-radius: 50%;
        border: 10px solid #fff;
        box-shadow: 0 1px 5px #ccc;
        overflow: hidden;
      }
      .login-btn {
        width: 100%;
      }
    }
  }
</style>
